package org.uned.agonzalo16.bitacora.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.uned.agonzalo16.bitacora.dao.MessageDao;
import org.uned.agonzalo16.bitacora.dao.UserDao;
import org.uned.agonzalo16.bitacora.domain.User;
import org.uned.agonzalo16.bitacora.service.security.AuthenticatedUser;
import org.uned.agonzalo16.bitacora.service.security.AuthenticationProvider;

@Configurable
@SuppressWarnings("serial")
public class MessagesNotReadTag extends TagSupport {

	private String var;

	@Autowired
	private transient UserDao userDao;

	@Autowired
	private transient AuthenticationProvider authenticationProvider;

	@Autowired
	private transient MessageDao messageDao;

	public void setVar(String var) {
		this.var = var;
	}

	@Override
	public int doStartTag() throws JspException {

		AuthenticatedUser principal = authenticationProvider.getCurrentUserDetails();
		User user = userDao.get(principal.getId());

		pageContext.setAttribute(var, messageDao.findNotReadByDestination(user));
		return SKIP_BODY;
	}
}
